

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>数据表格的重载 - 数据表格</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="{{ADMIN_PATH}}layui/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="{{ADMIN_PATH}}layui/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>组件</cite></a>
        <a><cite>数据表格</cite></a>
        <a><cite>数据表格的重载</cite></a>
    </div>
</div>

<div class="layui-fluid layui-form-pane" >
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">用户中心</div>

                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">


                        <div class="layui-inline">
                            <div class="layui-btn-group">
                            <button class="layui-btn layuiadmin-btn-list" id="export">立即备份</button>
                            <button class="layui-btn layui-btn-danger layuiadmin-btn-list" data-type="add">优化表</button>
                            <button class="layui-btn layui-btn-warm layuiadmin-btn-list" data-type="add">修复表</button>
                            </div>
                        </div>
                    </div>
                </div>


                <div class="layui-card-body">

                    <form id="export-form" method="post" action="{{url('admin/database/option/exprot')}}">

                    <table class="layui-table">
                        <colgroup>
                            <col width="60">
                            <col >
                            <col width="150">
                            <col width="200">
                            <col width="200">
                            <col width="200">
                            <col width="160">
                        </colgroup>
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>表名</th>
                            <th>表名称</th>
                            <th>数据量</th>
                            <th>数据大小</th>
                            <th>创建时间</th>
                            <th>备份状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($list as $k=>$v)
                        <tr>

                            <td><input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{{$v['name']}}"></td>
                            <td>{{$v['name']}}</td>
                            <td>{{$v['comment']}}</td>
                            <td>{{$v['rows']}}</td>
                            <td>{{format_bytes($v['data_length'])}}</td>
                            <td>{{$v['create_time']}}</td>
                            <td class="info">未备份</td>
                            <td> <div class="layui-btn-group">
                                    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">优化表</button>
                                    <button class="layui-btn layui-btn-xs layui-btn-warm" lay-event="opgg">修复表</button>
                                </div></td>
                        </tr>
                            @endforeach

                        </tbody>
                    </table>
                    </form>

                </div>
            </div>

        </div>
    </div>
</div>

<script src="{{ADMIN_PATH}}layui/layuiadmin/layui/layui.all.js"></script>
<script src="{{ADMIN_PATH}}jquery/dist/jquery.js?{{time()}}"></script>
<script type="text/javascript">
    (function($){
        var that=this;
        var $form = $("#export-form"), $export = $("#export"), tables
        $optimize = $("#optimize"), $repair = $("#repair");

        $optimize.add($repair).click(function(){
            $.post(this.href, $form.serialize(), function(data){
                if(data.status==200){
                    updateAlert(data.info,'alert-success');
                } else {
                    updateAlert(data.info,'alert-error');
                }
                setTimeout(function(){
                    $('#top-alert').find('button').click();
                    $(that).removeClass('disabled').prop('disabled',false);
                },1500);
            }, "json");
            return false;
        });

        $export.click(function(){
            $export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function(data){
                    if(data.status==200){
                        tables = data.tables;
                        $export.html(data.msg + "开始备份，请不要关闭本页面！");
                        backup(data.tab);
                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                    } else {
                        layer.msg(data.message,{
                            offset: '15px'
                            , icon: 2
                        });
                        $export.parent().children().removeClass("disabled");
                        $export.html("立即备份");
                        setTimeout(function(){
                            $('#top-alert').find('button').click();
                            $(that).removeClass('disabled').prop('disabled',false);
                        },1500);
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");

            $.get($form.attr("action"), tab, function(data){
                if(data.status==200){
                    showmsg(tab.id, data.msg);

                    if(!$.isPlainObject(data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    layer.msg(data.message,{
                        offset: '15px'
                        , icon: 2
                    });
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function(){
                        $('#top-alert').find('button').click();
                        $(that).removeClass('disabled').prop('disabled',false);
                    },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("li").find(".info").html(msg);
        }
    })(jQuery);
</script>
</body>
</html>